package com.probiz.estore.common.model.product;

public class PriceInterval {

	private final static int MIN_VALUE = Integer.MIN_VALUE;
	private final static int MAX_VALUE = Integer.MAX_VALUE;
	
	private String 	expression;		//区间表达式，例如(2,99]代表大于2少于或等于99
	private String 	label;			//能代表该区间表达式的文字，用于页面显示
	private int 	minValue;		//例如(2,99]，反回2
	private int 	maxValue;		//例如(2,99]，反回99
	private boolean	isLeftClosed;	//是否左闭区间
	private boolean	isRightClosed;	//是否右闭区间
	
	public PriceInterval(String exp) {
		if (!testExpression(exp)) {
			return;
		}
		parse(exp);
	}
	
	private void parse(String exp) {
		expression = exp.trim();
		
		isLeftClosed = expression.startsWith("[");
		isRightClosed = expression.endsWith("]");
		
		String min = expression.substring(1, expression.indexOf(","));
		String max = expression.substring(expression.indexOf(",") + 1, expression.length() - 1);
		
		if (min == null || min.length() < 1) {
			minValue = MIN_VALUE;
		} else {
			minValue = Integer.valueOf(min).intValue();
		}
		if (max == null || max.length() < 1) {
			maxValue = MAX_VALUE;
		} else {
			maxValue = Integer.valueOf(max).intValue();
		}
		
		if (minValue == MIN_VALUE) {
			label = "1-" + maxValue;
		} else if (maxValue == MAX_VALUE) {
			label = minValue + " or above";
		} else {
			label = minValue + " - " + maxValue;
		}
	}
	
	public static boolean isValidExpression(String exp) {
		return testExpression(exp);
	}
	
	private static boolean testExpression(String exp) {
		if (exp == null || exp.length() < 1) {
			return false;
		}
		
		if (!exp.startsWith("[") && !exp.startsWith("(") ) {
			return false;
		}
		
		if (!exp.endsWith("]") && !exp.endsWith(")")) {
			return false;
		}
		
		if (!exp.contains(",")) {
			return false;
		}
		
		String min = exp.substring(1, exp.indexOf(","));
		String max = exp.substring(exp.indexOf(",") + 1, exp.length() - 1);
		try {
			if (min != null && min.length() > 0) {
				Integer.parseInt(min);
			}
			if (max != null && max.length() > 0) {
				Integer.parseInt(max);
			}
		} catch (NumberFormatException e) {
			return false;
		}
		
		return true;
	}

	public String getExpression() {
		return expression;
	}

	public String getLabel() {
		return label;
	}


	public int getMinValue() {
		return minValue;
	}

	public int getMaxValue() {
		return maxValue;
	}

	//是否左闭区间，例如[1,2)反回true
	public boolean isLeftClosed() {
		return isLeftClosed;
	}
	
	//是否左开区间，例如[1,2)反回false
	public boolean isLeftOpenedd() {
		return !isLeftClosed;
	}

	//是否右闭区间，例如[1,2)反回false
	public boolean isRightClosed() {
		return isRightClosed;
	}
	
	//是否右开区间，例如[1,2)反回true
	public boolean isRightOpened() {
		return !isRightClosed;
	}
	
	//区间是否左无限,例如(负无穷,a)属于left-unbouned
	public boolean isLeftUnbouned() {
		return minValue == MIN_VALUE;
	}
	
	//区间是否右无限,例如(a,正无穷)属于right-unbouned
	public boolean isRightUnbounded() {
		return maxValue == MAX_VALUE;
	}

	public static void main(String[] args) {
		String exp1 = "(111,999]";
		PriceInterval p1 = new PriceInterval(exp1);
		System.out.println("1 exp: " + exp1);
		System.out.println("1 label: " + p1.getLabel());
		System.out.println("1 min: " + p1.getMaxValue());
		System.out.println("1 max: " + p1.getMaxValue());
		System.out.println("1 left closed: " + p1.isLeftClosed());
		System.out.println("1 right opened: " + p1.isRightOpened());

		String exp2 = "[556,]";
		PriceInterval p2 = new PriceInterval(exp2);
		System.out.println("2 exp: " + exp2);
		System.out.println("2 label: " + p2.getLabel());
		System.out.println("2 min: " + p2.getMaxValue());
		System.out.println("2 max: " + p2.getMaxValue());
		System.out.println("2 left closed: " + p2.isLeftClosed());
		System.out.println("2 right opened: " + p2.isRightOpened());
		
		String exp3 = "(,1234)";
		PriceInterval p3 = new PriceInterval(exp3);
		System.out.println("3 exp: " + exp3);
		System.out.println("3 label: " + p3.getLabel());
		System.out.println("3 min: " + p3.getMaxValue());
		System.out.println("3 max: " + p3.getMaxValue());
		System.out.println("3 left closed: " + p3.isLeftClosed());
		System.out.println("3 right opened: " + p3.isRightOpened());

	}
}
